<?php
/** 
*
* @author Tom Martin (Poppertom69) 
*
* @package acp
* @version $Id: acp_post_bot.php 31M 2007-08-05 01:09:43Z (local) $
* @copyright (c) 2007 Poppertom69
* @license http://opensource.org/licenses/gpl-license.php GNU Public License 
*
*/

/**
* acp_post_bot
* Post Bot
* @package acp
*/
class acp_post_bot
{
	var $u_action;

	function main($id, $mode)
	{
		global $config, $db, $user, $auth, $template;
		global $phpbb_root_path, $phpbb_admin_path, $phpEx;



		include($phpbb_root_path . 'includes/acp/info/acp_post_bot.' . $phpEx);
		
		$username		= utf8_normalize_nfc(request_var('username', '', true));
		$username_wel		= utf8_normalize_nfc(request_var('username_wel', '', true));
		$username_birth		= utf8_normalize_nfc(request_var('username_birth', '', true));
		$user_id		= request_var('u', 0);
		$approve		= request_var('post', 0);
		$enable_por		= request_var('enable_por', 0);
		$reg_post_forum	= request_var('r_f', 0);
		$subject		= request_var('subject', '', true);
		$message		= utf8_normalize_nfc (request_var('message', '', true));
		$enable_birth		= request_var('enable_birth', 0);
		$birth_post_forum	= request_var('birth_f', 0);
		$birth_subject		= request_var('birth_subject', '', true);
		$birth_message		= utf8_normalize_nfc (request_var('birth_message', '', true));
		$submit 		= (isset($_POST['submit'])) ? true : false;
		

		$user->add_lang(array('posting', 'ucp', 'acp/users'));

	if (!$username && !$user_id)
		{	
		
		$sql = 'SELECT post_bot_value
				FROM ' . POST_BOT_TABLE . "
				WHERE post_bot_config = 'post_bot_id'";
			$result = $db->sql_query($sql);
			$post_bot_id =  $db->sql_fetchfield('post_bot_value');
			$db->sql_freeresult($result);
			
				
		$sql = 'SELECT username
				FROM ' . USERS_TABLE . "
				WHERE user_id =  '$post_bot_id'";
			$result = $db->sql_query($sql);
			$username_bot =  $db->sql_fetchfield('username');
			$db->sql_freeresult($result);
			
			$sql = 'SELECT post_bot_value
				FROM ' . POST_BOT_TABLE . "
				WHERE post_bot_config = 'posts_need_approving'";
			$result = $db->sql_query($sql);
			$posts_need_approving =  $db->sql_fetchfield('post_bot_value');
			$db->sql_freeresult($result);
			
			$sql = 'SELECT post_bot_value
				FROM ' . POST_BOT_TABLE . "
				WHERE post_bot_config = 'welcome_enable'";
			$result = $db->sql_query($sql);
			$welcome_enable =  $db->sql_fetchfield('post_bot_value');
			$db->sql_freeresult($result);
			
			$sql = 'SELECT post_bot_value
				FROM ' . POST_BOT_TABLE . "
				WHERE post_bot_config = 'welcome_forum'";
			$result = $db->sql_query($sql);
			$welcome_forum =  $db->sql_fetchfield('post_bot_value');
			$db->sql_freeresult($result);
			
			$sql = 'SELECT post_bot_value
				FROM ' . POST_BOT_TABLE . "
				WHERE post_bot_config = 'welcome_message'";
			$result = $db->sql_query($sql);
			$welcome_message =  $db->sql_fetchfield('post_bot_value');
			$db->sql_freeresult($result);
			
			$sql = 'SELECT post_bot_value
				FROM ' . POST_BOT_TABLE . "
				WHERE post_bot_config = 'welcome_subject'";
			$result = $db->sql_query($sql);
			$welcome_subject =  $db->sql_fetchfield('post_bot_value');
			$db->sql_freeresult($result);
			
			$sql = 'SELECT post_bot_value
				FROM ' . POST_BOT_TABLE . "
				WHERE post_bot_config = 'welcome_poster_id'";
			$result = $db->sql_query($sql);
			$welcome_poster_id =  $db->sql_fetchfield('post_bot_value');
			$db->sql_freeresult($result);
			
			$sql = 'SELECT username
				FROM ' . USERS_TABLE . "
				WHERE user_id =  '$welcome_poster_id'";
			$result = $db->sql_query($sql);
			$welcome_bot =  $db->sql_fetchfield('username');
			$db->sql_freeresult($result);
			
			$sql = 'SELECT post_bot_value
				FROM ' . POST_BOT_TABLE . "
				WHERE post_bot_config = 'birthday_enable'";
			$result = $db->sql_query($sql);
			$birth_enable =  $db->sql_fetchfield('post_bot_value');
			$db->sql_freeresult($result);
			
			$sql = 'SELECT post_bot_value
				FROM ' . POST_BOT_TABLE . "
				WHERE post_bot_config = 'birthday_forum'";
			$result = $db->sql_query($sql);
			$birth_forum =  $db->sql_fetchfield('post_bot_value');
			$db->sql_freeresult($result);
			
			$sql = 'SELECT post_bot_value
				FROM ' . POST_BOT_TABLE . "
				WHERE post_bot_config = 'birthday_message'";
			$result = $db->sql_query($sql);
			$birth_message =  $db->sql_fetchfield('post_bot_value');
			$db->sql_freeresult($result);
			
			$sql = 'SELECT post_bot_value
				FROM ' . POST_BOT_TABLE . "
				WHERE post_bot_config = 'birthday_subject'";
			$result = $db->sql_query($sql);
			$birth_subject =  $db->sql_fetchfield('post_bot_value');
			$db->sql_freeresult($result);
			
			$sql = 'SELECT post_bot_value
				FROM ' . POST_BOT_TABLE . "
				WHERE post_bot_config = 'birthday_poster_id'";
			$result = $db->sql_query($sql);
			$birth_poster_id =  $db->sql_fetchfield('post_bot_value');
			$db->sql_freeresult($result);
			
			$sql = 'SELECT username
				FROM ' . USERS_TABLE . "
				WHERE user_id =  '$birth_poster_id'";
			$result = $db->sql_query($sql);
			$birth_bot =  $db->sql_fetchfield('username');
			$db->sql_freeresult($result);
						
						
						
		if ($posts_need_approving == 1){
		$post_approve = 1;
		}else{
		$post_approve = 0;
		}	
		
		if ($welcome_enable == 1){
		$por_enable = 1;
		}else{
		$por_enable = 0;
		}	
		
		if ($birth_enable == 1){
		$pob_enable = 1;
		}else{
		$pob_enable = 0;
		}
		
		$template->assign_vars(array(
		
						'USERNAME_BOT'	=> $username_bot,
						'POST_APPROVE'	=> $post_approve,
						'U_FIND_USERNAME'	=> append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=searchuser&amp;form=select_user&amp;field=username&amp;select_single=true'),
						
						'WELCOME_POSTER'	=> $welcome_bot,
						'U_FIND_USERNAME_WEL'	=> append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=searchuser&amp;form=select_user&amp;field=username_wel&amp;select_single=true'),													
						'ENABLE_POR'	=> $por_enable,
						'POR_FORUM'	=> $welcome_forum,
						'POR_SUBJECT'	=> $welcome_subject,
						'POR_MESSAGE'	=> utf8_normalize_nfc ($welcome_message),
						
						'BIRTH_POSTER'	=> $birth_bot,
						'U_FIND_USERNAME_BIRTH'	=> append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=searchuser&amp;form=select_user&amp;field=username_birth&amp;select_single=true'),													
						'ENABLE_BIRTH'	=> $pob_enable,
						'BIRTH_FORUM'	=> $birth_forum,
						'BIRTH_SUBJECT'	=> $birth_subject,
						'BIRTH_MESSAGE'	=> utf8_normalize_nfc ($birth_message),
						
						'USERNAME_VAR'	=> $user->data['username'],
						'USER_ID_VAR'	=> $user->data['user_id'],
						'SITE_NAME_VAR'	=> $config['sitename'],
						'SITE_DESC_VAR'	=> $config['site_desc'],
						'ROOT_PATH_VAR'	=> $phpbb_root_path,
											

			
			)
		);
		
	
		}
		
		if ($submit )
		{
			$sql = 'SELECT user_id
				FROM ' . USERS_TABLE . "
				WHERE username_clean = '" . $db->sql_escape(utf8_clean_string($username)) . "'";
			$result = $db->sql_query($sql);
			$user_id = (int) $db->sql_fetchfield('user_id');
			$db->sql_freeresult($result);
			
			$sql = 'SELECT user_id
				FROM ' . USERS_TABLE . "
				WHERE username_clean = '" . $db->sql_escape(utf8_clean_string($username_wel)) . "'";
			$result = $db->sql_query($sql);
			$user_id_wel = (int) $db->sql_fetchfield('user_id');
			$db->sql_freeresult($result);
			
			$sql = 'SELECT user_id
				FROM ' . USERS_TABLE . "
				WHERE username_clean = '" . $db->sql_escape(utf8_clean_string($username_birth)) . "'";
			$result = $db->sql_query($sql);
			$user_id_birth = (int) $db->sql_fetchfield('user_id');
			$db->sql_freeresult($result);
			
			
			
	$sql = 'UPDATE ' . POST_BOT_TABLE . " SET post_bot_value = '$user_id' WHERE post_bot_config = 'post_bot_id'";
	$db->sql_query($sql);
	
	$sql = 'UPDATE ' . POST_BOT_TABLE . " SET post_bot_value = '$approve' WHERE post_bot_config = 'posts_need_approving'";
	$db->sql_query($sql);
	
	$sql = 'UPDATE ' . POST_BOT_TABLE . " SET post_bot_value = '$enable_por' WHERE post_bot_config = 'welcome_enable'";
	$db->sql_query($sql);
	
	$sql = 'UPDATE ' . POST_BOT_TABLE . " SET post_bot_value = '$reg_post_forum' WHERE post_bot_config = 'welcome_forum'";
	$db->sql_query($sql);
	
	$sql = 'UPDATE ' . POST_BOT_TABLE . " SET post_bot_value = '$message' WHERE post_bot_config = 'welcome_message'";
	$db->sql_query($sql);
	
	$sql = 'UPDATE ' . POST_BOT_TABLE . " SET post_bot_value = '$subject' WHERE post_bot_config = 'welcome_subject'";
	$db->sql_query($sql);
	
	$sql = 'UPDATE ' . POST_BOT_TABLE . " SET post_bot_value = '$user_id_wel' WHERE post_bot_config = 'welcome_poster_id'";
	$db->sql_query($sql);
	
	$sql = 'UPDATE ' . POST_BOT_TABLE . " SET post_bot_value = '$enable_birth' WHERE post_bot_config = 'birthday_enable'";
	$db->sql_query($sql);
	
	$sql = 'UPDATE ' . POST_BOT_TABLE . " SET post_bot_value = '$birth_post_forum' WHERE post_bot_config = 'birthday_forum'";
	$db->sql_query($sql);
	
	$sql = 'UPDATE ' . POST_BOT_TABLE . " SET post_bot_value = '$birth_message' WHERE post_bot_config = 'birthday_message'";
	$db->sql_query($sql);
	
	$sql = 'UPDATE ' . POST_BOT_TABLE . " SET post_bot_value = '$birth_subject' WHERE post_bot_config = 'birthday_subject'";
	$db->sql_query($sql);
	
	$sql = 'UPDATE ' . POST_BOT_TABLE . " SET post_bot_value = '$user_id_birth' WHERE post_bot_config = 'birthday_poster_id'";
	$db->sql_query($sql);
		

			if (!$user_id)
			{
				trigger_error($user->lang['NO_USER'] . adm_back_link($this->u_action), E_USER_WARNING);
			}
	
	add_log('admin', 'LOG_CONFIG_' . strtoupper($mode));

			trigger_error($user->lang['CONFIG_UPDATED'] . adm_back_link($this->u_action));

		}

	
		$this->tpl_name = 'acp_post_bot';
		$this->page_title = 'ACP_POST_BOT';
	}
}

?>